<?php class update_historys extends Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function truncate(){
    	return $this->db->truncate('update_historys');
    }
    
    function select_by_version($update,$arr){
      if($update==0){
      	$this->db->where_in('version_id', $arr);
      }else{
      	$this->db->where_not_in('version_id', $arr);
      }
       $query = $this->db->get('update_historys');       
       $result = $query->result(); 
       return $result;
    }

    function insert_for_excel($array){
    	$this->version_id = $array['version_id'];
    	$this->category_id = $array['category_id'];
    	$this->mod_id = $array['mod_id'];
    	$this->codes = $array['codes'];
    	$this->company_id = $array['company_id'];
    	$this->pre_version = $array['pre_version'];
    	$this->count = $array['count'];
    	$this->updatetime = $array['updatetime'];
    	$this->createtime = time();
    	$re = $this->select_by_array(array('codes'=>$array['codes'],'mod_id'=>$array['mod_id'],'category_id'=>$array['category_id'],'pre_version'=>$array['pre_version'],'version_id'=>$array['version_id'],'company_id'=>$array['company_id']));
    	if(count($re)==0){
    		$this->count = $array['count'];
    		$this->db->insert('update_historys',$this);
    		$result = $this->db->insert_id();
    	}else{
    		$count = $re[0]->count+$array['count'];
    		$this->update($re[0]->send_id,array('count'=>$count));
    		$result = $re[0]->send_id;
    	}
    	return $result;
    }
    
    
    function insert($data){
       $result = $this->db->insert('update_historys',$data);
       return $result;
    }
    
    function select($id,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($id!=""){
       	  $this->db->where(array('send_id'=>$id));
       }
       $this->db->order_by('send_id','desc');
       $query = $this->db->get('update_historys');
       
       $result = $query->result(); 
       return $result;
    }
    
    
    function select2($id,$num="",$offset=""){
    	$this->db->limit($num,$offset);
    	if($id!=""){
    		$this->db->where($id);
    	}
    	$this->db->order_by('send_id','desc');
    	$query = $this->db->get('update_historys');
    	 
    	$result = $query->result();
    	return $result;
    }
    
    
    function select_by_array($array){
       $this->db->where($array);
       $this->db->order_by('send_id','asc');
       $query = $this->db->get('update_historys');       
       $result = $query->result(); 
       return $result;
    }
    
    function getcom($id){
    	$this->db->select('company_id');
        $this->db->where(array('category_id'=>$id));
    	$this->db->distinct();
        $query = $this->db->get('update_historys');
        $result = $query->result(); 
        return $result;
    }
    
    
    
    function getlast($version_id,$id){
       $this->db->where(array('category_id'=>$id,'version_id'=>$version_id));
       $this->db->select_sum('count');
       $query = $this->db->get('update_historys');
       $result = $query->result(); 
       return $result;
    }
    
    
    function getcount($id,$version_id=''){
        $where['category_id']=$id;
    	if($version_id!=""){
    	 $where['version_id']=$version_id;
    	}
        $this->db->where($where);
    	$this->db->select_sum('count');
        $query = $this->db->get('update_historys');
        $result = $query->result(); 
        return $result;
    }
    
    
    function update($id,$array){
       $result = $this->db->update('update_historys',$array,array('send_id'=>$id));
       return $result;
    }
    
    function del($id){
       $result = $this->db->delete('update_historys',array('send_id'=>$id));
       return $result;
    }
}